import 'package:flutter/material.dart';
import 'package:icons_plus/icons_plus.dart';
import 'package:super_study_language_tools/model/model_user_album_media.dart';
import 'package:super_study_language_tools/service/service_user_album_media.dart';

import '../widget/widget_album_media.dart';
import 'page_player.dart';

class PageAlbumMediaList extends StatefulWidget {
  PageAlbumMediaList({super.key, required this.albumId});

  final int albumId;

  @override
  State<PageAlbumMediaList> createState() => _PageAlbumMediaListState();
}

class _PageAlbumMediaListState extends State<PageAlbumMediaList> {
  List<ModelUserAlbumMedia> _list = [];

  int _listType = 0;

  int _allQty = 0;
  int _needReviewQty = 0;

  TextStyle _selListTextStyle = new TextStyle(color: Colors.blue);
  TextStyle _normalListTextStyle = new TextStyle(color: Colors.grey);

  var _sortCol = 0;

  @override
  void initState() {
    super.initState();

    op_refresh();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[300],
        appBar: AppBar(
          backgroundColor: Colors.grey[300],
          title: Text("专辑媒体列表"),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.refresh),
              onPressed: () {
                op("op_refresh");
              },
            ),
          ],
        ),
        body: Column(
          children: [
            buildTabs(),
            buildTable(),
          ],
        ));
  }

  buildTabs() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        TextButton(
          child: Text(
            "所有 ${_allQty}",
            style: _listType == 0 ? _selListTextStyle : _normalListTextStyle,
          ),
          onPressed: () {
            _listType = 0;
            setState(() {});
          },
        ),
        TextButton(
            child: Text(
              "需要复习 ${_needReviewQty}",
              style: _listType == 1 ? _selListTextStyle : _normalListTextStyle,
            ),
            onPressed: () {
              _listType = 1;
              setState(() {});
            }),
      ],
    );
  }

  buildTable() {

    var screenWidth = MediaQuery.of(context).size.width;
    print("screenWidth=${screenWidth}");
    if(screenWidth<800)
    {
      return Expanded(
        child: SingleChildScrollView(
          child: DataTable(
              sortColumnIndex: _sortCol,
              sortAscending: true,
              columns: [
                DataColumn(
                  label: Text("名字"),
                  onSort: (a, b) {
                    print("onSort:${a},${b}");
                    _sortCol = a;
                    setState(() {});
                  },
                ),
                // DataColumn(
                //     label: Text("句子数"),
                //     onSort: (a, b) {
                //       print("onSort:${a},${b}");
                //       _sortCol = a;
                //       setState(() {});
                //     }),
                // DataColumn(label: Text("完成数")),
                // DataColumn(label: Text("待复习")),
                // DataColumn(label: Text("忽略数")),
                // DataColumn(label: Text("单词量")),
                // DataColumn(label: Text("语速")),
                DataColumn(label: Text("操作")),
              ],
              rows: _list
                  .map((media) => DataRow(cells: [
                DataCell(Text(media.fileName!)),
                // DataCell(Text("${media.sentenceQty}")),
                // DataCell(Text("${media.finishQty}")),
                // DataCell(Text("${media.reviewQty}")),
                // DataCell(Text("${media.ignoreQty}")),
                // DataCell(Text("${media.wordQty}")),
                // DataCell(Text("${media.speed}")),
                DataCell(Row(
                  children: [
                    IconButton(
                      onPressed: () {
                        op("op_study", media);
                      },
                      icon: Icon(Bootstrap.book),
                      tooltip: "开始学习",
                    ),
                  ],
                ))
              ]))
                  .toList()),
        ),
      );
    }
    else
      {
        return Expanded(
          child: SingleChildScrollView(
            child: DataTable(
                sortColumnIndex: _sortCol,
                sortAscending: true,
                columns: [
                  DataColumn(
                    label: Text("名字"),
                    onSort: (a, b) {
                      print("onSort:${a},${b}");
                      _sortCol = a;
                      setState(() {});
                    },
                  ),
                  DataColumn(
                      label: Text("句子数"),
                      onSort: (a, b) {
                        print("onSort:${a},${b}");
                        _sortCol = a;
                        setState(() {});
                      }),
                  DataColumn(label: Text("完成数")),
                  DataColumn(label: Text("待复习")),
                  DataColumn(label: Text("忽略数")),
                  DataColumn(label: Text("单词量")),
                  DataColumn(label: Text("语速")),
                  DataColumn(label: Text("操作")),
                ],
                rows: _list
                    .map((media) => DataRow(cells: [
                  DataCell(Text(media.fileName!)),
                  DataCell(Text("${media.sentenceQty}")),
                  DataCell(Text("${media.finishQty}")),
                  DataCell(Text("${media.reviewQty}")),
                  DataCell(Text("${media.ignoreQty}")),
                  DataCell(Text("${media.wordQty}")),
                  DataCell(Text("${media.speed}")),
                  DataCell(Row(
                    children: [
                      IconButton(
                        onPressed: () {
                          op("op_study", media);
                        },
                        icon: Icon(Bootstrap.book),
                        tooltip: "开始学习",
                      ),
                    ],
                  ))
                ]))
                    .toList()),
          ),
        );
      }


  }

  void op(String s, [data]) {
    print(s);
    switch (s) {
      case "op_study":
        op_study(data);
        break;
      case "op_refresh":
        op_refresh();
        break;
    }
  }

  op_study(media) {
    Widget w = PagePlayer(mode: 1, media: media);
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return w;
    })).then((value) {
      print("back!!");
      // widget.onBack(0);
      op_refresh();
    });
  }

  void op_refresh() {
    // 1. 请求网络数据

    // 2. 读取数据库
    ServiceUserAlbumMedia.listByAlbumId(widget.albumId).then((value) {
      this._list = value;
      setState(() {});
    });
  }
}
